package com.example.yls.demoa;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.nio.Buffer;

public class MainActivity extends AppCompatActivity {
         private Button btn_createDatabase;
         private Button btn_createTable;
         private  SQLiteDatabase db;
         private EditText editName;
         private EditText editNo;
         private EditText editAge;
         private Button btn_insert;
         private Button btn_query;
         private TextView txt_show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initBundler();

        btn_createDatabaseEvent();

        btn_createTableEvent();
       btn_insertEvent();

       btn_queryEvent();

    }

    private void btn_queryEvent() {
        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    readDatabase();
            }
        });

    }

    private void readDatabase() {
              if (db==null){
                  createDatabase();
              }
          Cursor cursor= db.query("Student",
                null,null,
                null,null,
                null,null);
        StringBuffer buffer=new StringBuffer();
              for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                  String name=cursor.getString(1);
                  String no=cursor.getString(0);
                  int age=cursor.getInt(2);



                  buffer.append(name+"     "+no+"      "+age+"\n");

              }
                  txt_show.setText(buffer);



    }

    private void btn_insertEvent() {
        btn_insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String name=editName.getText().toString();
                String no=editNo.getText().toString();
                int age=Integer.valueOf(editAge.getText().toString());
                insertEvent(no,name,age);
            }
        });


    }

    private void insertEvent(String no,String name,int age) {
        if (db==null) {
            createDatabase();
        }

             String sql="insert into student values(+'"+no+"','"+name+"',"+age+")";
        Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show();
             db.execSQL(sql);
    }

    private void btn_createTableEvent() {
        btn_createTable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createTable();
            }
        });


    }

    private void createTable() {
              if (db==null) {
                  createDatabase();
              }






                    String sql="create table if not exists student " +
                          " (stuno varchar(20),"+"name varchar(20),"+"age int);";
                  db.execSQL(sql);

        Toast.makeText(this, "创建成功！", Toast.LENGTH_SHORT).show();



    }

    private void btn_createDatabaseEvent() {
        btn_createDatabase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createDatabase();
            }
        });


    }

    private void createDatabase() {
           String path=getFilesDir().getAbsolutePath()+
                   File.separator+"stu.db";
         db = SQLiteDatabase.openOrCreateDatabase(path, null);




    }

    private void initBundler() {
          editNo=findViewById(R.id.edit_no);
          editName=findViewById(R.id.edit_name);
          editAge=findViewById(R.id.edit_age);
          btn_createDatabase=findViewById(R.id.btn_createDatabase);
          btn_createTable=findViewById(R.id.btn_createTable);
          btn_insert=findViewById(R.id.btn_insert);
          btn_query=findViewById(R.id.btn_query);
          txt_show=findViewById(R.id.txt_show);

    }
}
